package com.ly.test.config;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.springframework.stereotype.Component;

/**
 * 自定义注释生成器
 * @author liyang
 */
public class MyCommentGenerator extends DefaultCommentGenerator {

    public MyCommentGenerator() { super(); }

    @Override
    public void addFieldComment(Field field,
                                IntrospectedTable introspectedTable,
                                IntrospectedColumn introspectedColumn) {
        // 只生成数据库备注，不要默认的冗余注释
        if (introspectedColumn != null) {
            String remarks = introspectedColumn.getRemarks();
            if (remarks != null && !remarks.trim().isEmpty()) {
                field.addJavaDocLine("/** " + remarks + " */");
            }
        }
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
        super.addClassComment(innerClass, introspectedTable);
        // 给实体类加上表备注
        String remarks = introspectedTable.getRemarks();
        if (remarks != null && !remarks.trim().isEmpty()) {
            innerClass.addJavaDocLine("/** " + remarks + " */");
        }
    }
}
